package com.leyou.item.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exceptions.LyException;
import com.leyou.common.utils.BeanHelper;
import com.leyou.common.vo.PageResult;
import com.leyou.item.dto.BrandDTO;
import com.leyou.item.entity.Brand;
import com.leyou.item.mappers.BrandMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
@Transactional
@Slf4j
public class BrandService {
    @Autowired
    private BrandMapper brandMapper;

    public PageResult<BrandDTO> queryBrandByPage(Integer page, Integer rows, String key, String sortBy, Boolean desc) {
        // 分页
        Page<Object> pageInfo = PageHelper.startPage(page, rows);
        // 过滤条件
        Example example = new Example(Brand.class);
        if (StringUtils.isNoneBlank(key)) {
            example.createCriteria().orLike("name", "%" + key + "%")
                    .orEqualTo("letter", key.toUpperCase());
        }
        // 排序
        if (StringUtils.isNoneBlank(sortBy)) {
            String orderByClause = sortBy + (desc ? " DESC" : " ASC");
            example.setOrderByClause(orderByClause);// id desc
        }
        // 查询

        List<Brand> brands = brandMapper.selectByExample(example);

        // 判断是否为空
        if (CollectionUtils.isEmpty(brands)) {
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }
        // 解析分页结果
        /* PageInfo<Brand> info = new PageInfo<>(brands);*/

        // 转为BrandDTO
        List<BrandDTO> list = BeanHelper.copyWithCollection(brands, BrandDTO.class);

        // 返回
        return new PageResult<>(pageInfo.getTotal(), list);
    }


    public void savaBrand(BrandDTO brandDTO, List<Long> ids) {
        //首先把brandDTO数据转化为数据库认识的
        Brand brand = BeanHelper.copyProperties(brandDTO, Brand.class);
        brand.setId(null);
        int i = brandMapper.insertSelective(brand);
        if (i == 0) {
            throw new LyException(ExceptionEnum.INSERT_OPERATION_FAIL);
        }
        Integer count = brandMapper.insertCategory(brand.getId(), ids);
        if (count == 0) {
            throw new LyException(ExceptionEnum.INSERT_OPERATION_FAIL);
        }
    }

    /**
     * 根据id查询brand商品表的商品名字
     *
     * @param id
     * @return
     */
    public BrandDTO queryById(Long id) {
        Brand brand = brandMapper.selectByPrimaryKey(id);
        if (brand == null) {
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);


        }
        return BeanHelper.copyProperties(brand, BrandDTO.class);


    }

    /**
     * 通过传的类id找到品牌集合
     *
     * @param cid
     * @return
     */
    public List<BrandDTO> queryBrandByCategoryId(Long cid) {
        List<Brand> brands = brandMapper.queryBrandByCategoryId(cid);
        if (CollectionUtils.isEmpty(brands)) {

            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }
        return BeanHelper.copyWithCollection(brands, BrandDTO.class);
    }

    public List<BrandDTO> queryByIds(List<Long> idList) {
        List<Brand> brands = brandMapper.selectByIdList(idList);
        List<BrandDTO> brandDTOList = BeanHelper.copyWithCollection(brands, BrandDTO.class);
        return brandDTOList;
    }
}
